﻿using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.Text;

namespace S2_2.Models
{
    public class AppResult
    {
        public AppResult()
        {
            IsSuccess = true;
        }
        public AppResult(string message) : this(false, message)
        {

        }
        public AppResult(bool isSuccess, string message)
        {
            this.IsSuccess = isSuccess;
            this.Message = message;
        }
        /// <summary>
        /// 是否执行完成
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 消息,未完成状态（错误），错误消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 填充数据验证错误数据
        /// </summary>
        /// <param name="msd">数据验证字典</param>
        public void ModelError(ModelStateDictionary msd)
        {
            var sb = new StringBuilder();
            foreach (var key in msd.Keys)
            {
                //ModelState["product.ProductCategory"].Errors[0].ErrorMessage
                foreach (var item in msd[key].Errors)
                {
                    sb.AppendLine(item.ErrorMessage);
                    sb.AppendLine(item.Exception?.Message);
                }
            }
            IsSuccess = false;
            Message = sb.ToString();
        }
    }
    public class AppResult<T> : AppResult
    {
        public AppResult(bool isSuccess, string message) : base(isSuccess, message)
        {
        }
        public AppResult(T data) : this(true, string.Empty)
        {
            Data = data;
        }

        /// <summary>
        /// 返回数据
        /// </summary>
        public T Data { get; set; }

    }
}